const api = require('../../api/index')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    rocommendlist:[],
    newsong:[],
    listarr:[
      {
        name:"每日推荐",
        picUrl:"/asstes/icon1.png",
        id:1
      },
      {
        name:"歌单",
        picUrl:"/asstes/icon2.png",
        id:2
      },
      {
        name:"茵悦时刻",
        picUrl:"/asstes/icon3.png",
        id:3
      },
      {
        name:"排行榜",
        picUrl:"/asstes/icon4.png",
        id:4
      },
      {
        name:"小众曲目",
        picUrl:"/asstes/icon5.png",
        id:5
      },
    ],
    songcanok:[],
    sidebarSize:"-480"
  },
  // 跳转排行榜
  jumpcharts(e){
    let i = e.currentTarget.dataset.i
    if ( i == 4) {
      wx.switchTab({
        url: '/pages/charts/charts',
      })
    }
  },
  // 跳转搜索页
  jumpSearch(){
    wx.navigateTo({
      url: '/pages/search/index',
    })
  },
  // 跳转歌单详情页
  jumpSongdetails(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../songlistdetails/index?id=${id}`,
    })
  },
  // 跳转播放页
  playdata(e){
    app.globalData.curIndex = e.currentTarget.dataset.curindex
    app.globalData.list = this.data.newsong
    let ids = e.currentTarget.dataset.ids
    this.songcanplay(ids)
    // let success = this.data.songcanok.success
    // let msg = this.data.songcanok.message
    // if (!success) {
    //   wx.showModal({
    //     title: '提示',
    //     content: msg,
    //   })
    //   return
    // }
    wx.navigateTo({
      url: `../songdetail/songdetail?ids=${ids}`,
    })
  },
  // 判断歌曲是否可以播放
  songcanplay(ids){
    api.getsongok({id:ids})
    .then(
      content=>{
        this.setData({
            songcanok:content
        })
      }
    )
  },
  sidebarshow(){
    this.setData({
      sidebarSize:"0"
    })
  },
  sidebarhide(){
    this.setData({
      sidebarSize:"-480"
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 获取推荐歌单
    api.getrecommendList()
    .then(
        content => {
          let {code, result} = content;
          let rocommend = result
          rocommend = result.slice(0, 6)
          if(code == 200) {
           let arr =  rocommend.map(item=>{

              return {
                // xxx:item.xxx
                playCount:item.playCount.length > 8 ?(item.playCount / 100000000).toFixed(2) + '亿' : (item.playCount / 10000).toFixed(2) + '万',
                id:item.id,
                name:item.name,
                picUrl:item.picUrl,
                trackNumberUpdateTime:item.trackNumberUpdateTime,
                trackCount:item.trackCount
              }
            })
              // 赋值
              this.setData({
                rocommendlist: arr
              })
              
          }
        }
    )
   //最新音乐的数据
    api.getnewsong()
    .then(
      content => {
        let {code, result} = content;
        let newsong = result
        // console.log(newsong);
        // rocommend = result.slice(0, 6)
        if(code == 200) {
            // 赋值
            this.setData({
               newsong:newsong
            })
        }
      }
  )

 
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})